﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;

namespace MWTWorkUnitMgmtTool.DataAccess.TableGatewayRepository
{
    public class ProductionWorkunitRepository:IProductionWorkunitRepository
    {
        string _connectionString; 
        private ITableGatewayRepository _tableGatewayRepository; 

        public ProductionWorkunitRepository(ITableGatewayRepository tableGatewayRepository)
        {
            _tableGatewayRepository = tableGatewayRepository;
            _connectionString = ConfigurationManager.AppSettings["DB_SAPB1"].ToString(); 
        }

        #region IProductionWorkunitRepository Members

        public IEnumerable<System.Data.IDataRecord> GetProductionWorkunitsByBOM(string parentItemCode)
        {
            return _tableGatewayRepository.Retrieve("SELECT * FROM [@MWT_OPWU] WHERE U_MWT_Prnt_ItemCode = @ParentItemCode", _connectionString, delegate(SqlParameterCollection p)
            {
                p.Add("@ParentItemCode", System.Data.SqlDbType.VarChar).Value = parentItemCode;
            }
            );
        }

        public IEnumerable<System.Data.IDataRecord> GetProductionWorkunitsByWorkunitCode(string workUnitCode)
        {
            return _tableGatewayRepository.Retrieve("SELECT * FROM [@MWT_OPWU] WHERE Code = @WUCode", _connectionString, delegate(SqlParameterCollection p)
            {
                p.Add("@WUCode", System.Data.SqlDbType.VarChar).Value = workUnitCode;
            });
        }

        public int AddProductionWorkUnit(string WUCode, string WUName, string itemCode, string itemName, string parentItemCode, string facility)
        {
            return _tableGatewayRepository.Commit("INSERT INTO [@MWT_OPWU] VALUES(@WUCode, @WUName, @ItemCode, @ItemName, @ParentItemCode, @Facility)", _connectionString, delegate(SqlParameterCollection p)
            {
                p.Add("@WUCode", System.Data.SqlDbType.VarChar).Value = WUCode;
                p.Add("@WUName", System.Data.SqlDbType.VarChar).Value = WUName;
                p.Add("@ItemCode", System.Data.SqlDbType.VarChar).Value = itemCode;
                p.Add("@ItemName", System.Data.SqlDbType.VarChar).Value = itemName;
                p.Add("@ParentItemCode", System.Data.SqlDbType.VarChar).Value = parentItemCode;
                p.Add("@Facility", System.Data.SqlDbType.VarChar).Value = facility;
            });
        }

        public int UpdateProductionWorkunit(string wuCode, string wuName, string itemCode, string itemName, string parentItemCode, string facility)
        {
            return _tableGatewayRepository.Commit("UPDATE [@MWT_OPWU] SET Name=@WUName, U_MWT_ItemCode=@ItemCode, U_MWT_ItemName=@ItemName, U_MWT_Prnt_ItemCode=@ParentItemCode, U_Facility=@Facility WHERE Code=@WUCode", _connectionString, delegate(SqlParameterCollection p)
            {
                p.Add("@WUCode", System.Data.SqlDbType.VarChar).Value = wuCode;
                p.Add("@WUName", System.Data.SqlDbType.VarChar).Value = wuName;
                p.Add("@ItemCode", System.Data.SqlDbType.VarChar).Value = itemCode;
                p.Add("@ItemName", System.Data.SqlDbType.VarChar).Value = itemName;
                p.Add("@ParentItemCode", System.Data.SqlDbType.VarChar).Value = parentItemCode;
                p.Add("@Facility", System.Data.SqlDbType.VarChar).Value = facility;
            });
        }

        public int DeleteProductionWorkunitByWUCode(string wuCode)
        {
            return _tableGatewayRepository.Commit("DELETE FROM [@MWT_OPWU] WHERE Code=@WUCode", _connectionString, delegate(SqlParameterCollection p)
            {
                p.Add("@WUCode", System.Data.SqlDbType.VarChar).Value = wuCode; 
            });
        }

        public IEnumerable<System.Data.IDataRecord> GetAllItems()
        {
            return _tableGatewayRepository.Retrieve("SELECT ItemCode, ItemName FROM OITM", _connectionString, delegate(SqlParameterCollection p)
            {
            }); 
        }

        #endregion


     
    }
}
